package com.cloudwise.gproject.controller;

import com.cloudwise.gproject.entity.Business;
import com.cloudwise.gproject.entity.Upload;
import com.cloudwise.gproject.service.BusinessService;
import com.cloudwise.gproject.service.UploadService;
import com.cloudwise.gproject.util.Result;
import com.cloudwise.gproject.util.ReturnStatus;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * (Business)表控制层
 *
 * @author makejava
 * @since 2021-03-22 09:10:13
 */
@Controller
@RequestMapping("business")
public class BusinessController {
    /**
     * 服务对象
     */
    @Resource
    private BusinessService businessService;
    @Resource
    private UploadService uploadService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ResponseBody
    @RequestMapping("selectOne")
    public Result selectOne(Integer id) {
        return new Result(ReturnStatus.SUCCESS.getCode(),ReturnStatus.SUCCESS.getMessage(),
                1,this.businessService.queryById(id));
    }

    /**
     * @create by: 卫子健
     * @description: 跳转到部门管理页面dept.html?
     * @create time: 2021/3/19 19:45
     */
    @RequestMapping("toBusiness")
    public String toDept() {
        return "back/business";
    }

    /**
     * @return com.cloudwise.gproject.util.Result?
     * @create by: 卫子健
     * @description:
     * 1.分页条件查询所有部门列表?
     * 2.为progress.html的小table（businessTable）获取数据
     * @create time: 2021/3/19 20:35
     */
    @ResponseBody
    @RequestMapping("queryAll")
    public Result queryAll(Integer page, Integer limit, Business business, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Object businessId = session.getAttribute("kbusinessId");
        //获取行编辑栏“修改进度”传过来的数据
        business.setBusinessId((Integer) businessId);
        if (business.getStartDate() ==null || business.getStartDate()==""){
            business.setStartDate("2000-01-01 00:00:00");
        }
        if (business.getEndDate()=="" ||  business.getEndDate()==null){
            business.setEndDate("2100-01-01 00:00:00");
        }
        PageInfo pageInfo = businessService.queryAll(page, limit, business);
        session.removeAttribute("kbusinessId");
        return new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                pageInfo.getTotal(), pageInfo.getList());
    }
    /**
     * @param mv:
     * @return org.springframework.web.servlet.ModelAndView
     * @create by: IvanZ
     * @description : 显示客户信息和业务信息
     * @create time: 2020/12/23 15:56
     */
    @GetMapping("showCustomAndBusiness")
    public ModelAndView showCustomAndBusiness(int customId, ModelAndView mv,HttpServletRequest request) {
        HttpSession session = request.getSession();
        //点击business.HTML的申请人超链接后，将customId添加到session，为显示小表格提供数据
        session.setAttribute("ScustomId",customId);
        mv.addObject("customId", customId);
        mv.setViewName("back/business");
        return mv;
    }

    /**
     * 查询businessTopType所有类型
     * @return
     */
    @ResponseBody
    @RequestMapping("findAllDictionary")
    public Result findAllDictionary() {
        return new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                0, businessService.findAllDictionary());
    }
    /**
     * 修改数据
     *
     * @param business 实例对象
     * @return 实例对象
     */
    @ResponseBody
    @RequestMapping("update")
    public Result update(Business business, String uploadSourceName, String uploadDtName,HttpServletRequest request) {
        HttpSession session = request.getSession();
        business.setUpdateBy((String)session.getAttribute("username"));
        Upload upload = new Upload(business.getUploadId(), uploadSourceName, uploadDtName);
        if (uploadSourceName!=null&&uploadSourceName!=""&&uploadDtName!=null&&uploadDtName!=""){
            //判断该业务之前有没有附件，有则调用update方法，无则调用insert方法
            Upload upload1 = uploadService.queryById(business.getUploadId());
            if (upload1!=null){
                uploadService.update(upload);
            }else {
                Upload upload2 = uploadService.insert(upload);
                //将新添加的uploadid添加到business中
                business.setUploadId(upload2.getUploadId());
            }
        }
        //添加业务信息
        Business update = businessService.update(business);
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                update);
    }

    /** 
     * @create by: 卫子健
     * @description: 业务新增 
     * @create time: 2021/3/24 20:36
     * @param  
     * @return  
     */
    @ResponseBody
    @RequestMapping("insert")
    public Result insert(Business business,String uploadSourceName,String uploadDtName) {
        Upload upload = new Upload();
        upload.setSourceName(uploadSourceName);
        upload.setDestinationName(uploadDtName);
        Upload insert1 = uploadService.insert(upload);
        business.setUploadId(insert1.getUploadId());
        Business insert = this.businessService.insert(business);
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                insert);
    }
    /** 
     * @create by: 卫子健
     * @description: 获取首页数据，当天收入，当天访问量，当天业务数量 
     * @create time: 2021/3/22 9:44
     * @param  
     * @return  
     */
    @ResponseBody
    @RequestMapping("getIndexData")
    public Result getIndexData(){
//        int i = 1/0;
        List<Integer> indexData = businessService.getIndexData();
        return new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                0, indexData);
    }
    /** 
     * @create by: 卫子健
     * @description: 业务量走势图 
     * @create time: 2021/3/22 9:46
     * @param  
     * @return  
     */
    @ResponseBody
    @RequestMapping("showBusinessChart")
    public Result showBusinessChart(){
        Map<String, Object> map = businessService.showBusinessChart();
        return new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                0, map);
    }
    /** 
     * @create by: 卫子健
     * @description: 业绩排行榜 
     * @create time: 2021/3/22 9:46
     * @param  
     * @return  
     */
    @ResponseBody
    @RequestMapping("showPerformanceRanking")
    public Result showPerformanceRanking(){
        Map<String, Object> map = businessService.showPerformanceRanking();
        return new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                0, map);
    }

    /** 
     * @create by: 卫子健
     * @description: 各种业务类型的数量 
     * @create time: 2021/3/25 13:05
     * @param  
     * @return  
     */
    @ResponseBody
    @RequestMapping("businessEchart")
    public Result businessEchart(){
        List<Long> longs = businessService.businessEchart();
        return new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                0, longs);
    }

    /** 
     * @create by: 卫子健
     * @description: 按照代理人展示业务数量 
     * @create time: 2021/3/25 13:20
     * @param loginName 
     * @return com.cloudwise.gproject.util.Result 
     */
    @ResponseBody
    @RequestMapping("showBusinessChartByAgent")
    public Result showBusinessChartByAgent(String loginName){
        Map<String, Object> map = businessService.showBusinessChartByAgent(loginName);
        return new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                0, map);
    }

    /** 
     * @create by: 卫子健
     * @description: 按照日期区间显示业务数量 
     * @create time: 2021/3/25 13:22
     * @param startTime
     * @param endTime 
     * @return com.cloudwise.gproject.util.Result 
     */
    @ResponseBody
    @RequestMapping("showAgentChart")
    public Result showAgentChart(String startTime,String endTime){
        Map<String, Object> map = businessService.showAgentChart(startTime, endTime);
        return new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                0, map);
    }

}
